//
//  TF_PPTFunction.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/11/25.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit
typealias playbackBlock = ()  -> CGFloat

class TF_PPTFunction: UIView {
    @objc public let bottomView = UIView()
    @objc public let bottomViewBackgroundColor = UIView()
    
    @objc public let topView = UIView()
    @objc public let topViewBackgroundColor = UIView()
    
    @objc public let backBtn = TalkfunIncreaseButton()
    @objc public let refreshBtn = UIButton()
    @objc public let moreBtn = UIButton()
    @objc public let titleLab = UILabel()
    @objc public let stopBtn = UIButton()
    @objc public let startLab = UILabel()
    @objc public let endLab = UILabel()
    @objc public let slider = UISlider()
    @objc public let multipleBtn = UIButton()  //倍速
    @objc public let barrageBtn = UIButton()   //弹幕
    @objc public let chatBtn = UIButton()     // 聊天区域
    @objc public let chuangkou_Btn = UIButton() //窗口
    @objc public let networkBtn = UIButton()
    @objc public let chapter = UIButton()
    /**全屏*/
    @objc public let downloadBtn = UIButton()
    @objc public let fullScreenBtn = UIButton()
    @objc public var isOrientationLandscape:Bool = false
    @objc public var touch:Bool = false
    @objc public var isChange:Bool = false
 
    @objc public var sliderTapGestureBlock: ((CGFloat) -> ())?
   
    @objc public var sliderValueBlock: ((CGFloat) -> ())?
    @objc public var rateBlock: ((CGFloat) -> ())?
    @objc public var definitionBlock: ((String) -> ())?
    @objc public var sliderValueChangeBlock: ((CGFloat) -> ())?
    var timer:Timer!
    
    @objc public var multipleView:UIView = UIView()
    var isPlay:Bool = false
    var isPlaybackMode:Bool = false
    var gl = CAGradientLayer()
    var bottom_gl:CAGradientLayer?
    var multiplemaskView:UIView = UIView()
    @objc public var mulSubView:UIView = UIView()  //装蒙版和倍数的父类
    override init(frame: CGRect) {
        super.init(frame: frame)
        

        addSubview(self.bottomView)
        self.bottomView.addSubview(self.bottomViewBackgroundColor)
    

        addSubview(self.topView)
        self.topView.addSubview(self.topViewBackgroundColor)
        self.gl = self.getTopLayer()
        self.topViewBackgroundColor.layer.addSublayer(self.gl)
        
        self.mulSubView.backgroundColor = UIColor.clear
//        addSubview(self.mulSubView)
        
        self.multipleView.backgroundColor = UIColor(red: 0.08, green: 0.09, blue: 0.1, alpha: 0.8)
        addSubview(self.multipleView)
        
        self.multiplemaskView.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.1)
        self.mulSubView.addSubview(self.multiplemaskView)
        let maskTap = UITapGestureRecognizer.init(target: self, action: #selector(multiplemaskViewClickTap))
   
        self.multiplemaskView.isUserInteractionEnabled = true;
        self.multiplemaskView.addGestureRecognizer(maskTap)
        
        self.backBtn .setImage(UIImage.init(named: "HTLive.bundle/TF_return"), for: .normal)
        self.backBtn.tag = 200;
        self.topView .addSubview(self.backBtn)
     
        
        self.titleLab.font = UIFont.PingFang(size: 15)
        self.titleLab.text = "11111111"
        self.titleLab.textColor = UIColor.white
        self.topView .addSubview(self.titleLab)
        
        self.moreBtn.setImage(UIImage.init(named: "HTLive.bundle/largeLive_more"), for: .normal)
        self.moreBtn.tag = 2001;
        self.moreBtn.isHidden = true
        self.topView.addSubview(self.moreBtn);

        self.refreshBtn.setImage(UIImage.init(named: "HTLive.bundle/mask_icon_reflesh"), for: .normal)
        self.refreshBtn.tag = 203;
        self.refreshBtn.isHidden = false
        self.topView.addSubview(self.refreshBtn);
        
        self.downloadBtn.setImage(UIImage.init(named: "HTLive.bundle/xiazaiicon"), for: .normal)
        self.downloadBtn.tag = 203;
        self.downloadBtn.isHidden = false
        self.topView.addSubview(self.downloadBtn);
        
        self.stopBtn.setImage(UIImage.init(named: "HTLive.bundle/bofang1.png"), for: .selected)
        self.stopBtn.setImage(UIImage.init(named: "HTLive.bundle/zanting1.png"), for: .normal)
        self.stopBtn.tag = 20001;
        self.bottomView.addSubview(self.stopBtn)

        self.startLab.textColor = UIColor.white
        self.startLab.font = UIFont.PingFang(size: 12)
        self.startLab.text = "01:20:44"
        self.bottomView.addSubview(self.startLab)
        
        self.slider.thumbTintColor = UIColor(red: 0.25, green: 0.59, blue: 0.92, alpha: 1)
        self.slider.maximumValue = 1
        self.slider.minimumTrackTintColor = UIColor(red: 0.25, green: 0.59, blue: 0.92, alpha: 1)
        self.slider.maximumTrackTintColor = UIColor.white;
        self.bottomView.addSubview(self.slider)
        
        
        let sliderTap = UITapGestureRecognizer.init(target: self, action: #selector(TF_PPTFunction.sliderTap(_:)))
        self.slider.addGestureRecognizer(sliderTap)
        self.slider.isContinuous = true;
        self.slider.value = 0.0;
        self.slider.isUserInteractionEnabled = true;
        self.slider .addTarget(self, action: #selector(TF_PPTFunction.eventTouchChanged(_slider:)), for: .touchCancel)
       
        //移动过程中
        self.slider .addTarget(self, action: #selector(TF_PPTFunction.ControlEventValueChanged(_slider:)), for: .valueChanged)
        self.slider .addTarget(self, action: #selector(TF_PPTFunction.sliderThumbTouch(_slider:)), for: .touchDown)
        self.slider .addTarget(self, action: #selector(TF_PPTFunction.sliderThumbUnTouch(_slider:)), for: .touchUpInside)
        self.slider .addTarget(self, action: #selector(TF_PPTFunction.sliderThumbUnTouch(_slider:)), for: .touchUpOutside)


        self.slider.setThumbImage(UIImage.init(named: "HTLive.bundle/tiaojie"), for: .normal)
        self.slider.setThumbImage(UIImage.init(named: "HTLive.bundle/tiaojie"), for: .highlighted)
        
        self.endLab.textColor = UIColor.white
        self.endLab.font =  UIFont.PingFang(size: 12)
        self.endLab.text = "03:22:12"
        self.endLab.textAlignment = NSTextAlignment.center;
        self.bottomView.addSubview(self.endLab)
        
        self.chapter.setTitle("章节", for: .normal)
        self.chapter.titleLabel?.font = UIFont.PingFang(size: 16)
        self.chapter.setTitleColor(.white, for: .normal)
        self.bottomView.addSubview(self.chapter)
        
        self.multipleBtn.setTitle("1x", for: .normal)
        self.multipleBtn.titleLabel?.font = UIFont.PingFang(size: 18)
        self.multipleBtn.setTitleColor(.white, for: .normal)
        self.bottomView.addSubview(self.multipleBtn)
        
    
        self.chatBtn.setImage(UIImage.init(named: "HTLive.bundle/liaotianqu"), for: .normal)
        self.chatBtn.setImage(UIImage.init(named: "HTLive.bundle/liaotianquClose"), for: .selected)
        self.bottomView.addSubview(self.chatBtn)
        
        self.barrageBtn.setImage(UIImage.init(named: "HTLive.bundle/BarrageOn"), for: .selected)
        self.barrageBtn.setImage(UIImage.init(named: "HTLive.bundle/BarrageClose"), for: .normal)
      
        self.barrageBtn.tag = 2003;
        self.bottomView.addSubview(self.barrageBtn)
        
        self.chuangkou_Btn.setImage(UIImage.init(named: "HTLive.bundle/xianshichuangkou"), for: .normal)
        self.chuangkou_Btn.setImage(UIImage.init(named: "HTLive.bundle/hidenchuangkou"), for: .selected)
        self.chuangkou_Btn.tag = 2006;
        self.chuangkou_Btn.isHidden = true
        self.bottomView.addSubview(self.chuangkou_Btn)
        
        self.fullScreenBtn.setImage(UIImage.init(named: "HTLive.bundle/mask_icon_max"), for: .normal)
        self.fullScreenBtn.tag = 2007;
        self.bottomView.addSubview(self.fullScreenBtn)
  
    }
   
   @objc func multiplemaskViewClickTap()  {
       self.ShowmultipleView(show: false)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.topView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 44)
        self.topViewBackgroundColor.frame =  self.topView.bounds
        self.gl.frame = self.topViewBackgroundColor.bounds;
        
        
        self.backBtn.frame = CGRect(x:15, y:10, width:24, height:24)
        self.titleLab.frame = CGRect(x:  self.backBtn.frame.maxX+10, y:0, width:self.frame.size.width-130, height:40);
       
      
        
        if self.isOrientationLandscape {
            self.bottomView.frame = CGRect(x:0, y:self.frame.size.height-56, width:self.frame.size.width, height:56);
            self.moreBtn.isHidden = false;
            self.moreBtn.frame = CGRect(x:self.frame.size.width-40-24, y:10, width:24, height:24)
            self.stopBtn.frame = CGRect(x:40, y:(self.bottomView.frame.size.height-24)/2.0, width:24, height:24);
//            self.multiplemaskView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width-280, height: self.frame.size.height)
//            self.multipleView.frame = CGRect(x: self.frame.size.width-280, y: 0, width: 280, height: self.frame.size.height)
        } else {
            self.bottomView.frame = CGRect(x:0, y:self.frame.size.height-44, width:self.frame.size.width, height:44);
            self.moreBtn.isHidden = true;
            self.moreBtn.frame = CGRect(x:self.frame.size.width, y:10, width:24, height:24)
            self.stopBtn.frame = CGRect(x:22, y:(self.bottomView.frame.size.height-24)/2.0, width:24, height:24);
//            self.multiplemaskView.frame = CGRect(x: 0, y: 0, width:self.frame.size.width-88-30, height: self.frame.size.height)
//            self.multipleView.frame = CGRect(x: self.frame.size.width-88-30, y: 0, width: 88, height: self.frame.size.height)
        }
        self.bottomViewBackgroundColor.frame = self.bottomView.bounds;
        self.getBottomLayer()
        self.refreshBtn.frame = CGRect(x:self.moreBtn.frame.origin.x-32-24, y:10, width:24, height:24)
        self.downloadBtn.frame = CGRect(x:self.refreshBtn.frame.origin.x-32-24, y:10, width:24, height:24)
        self.startLab.frame = CGRect(x:self.stopBtn.frame.maxX+18, y:(self.bottomView.frame.size.height-20)/2.0, width:50, height:20);
        self.fullScreenBtn.frame = CGRect(x:self.frame.size.width-36, y:(self.bottomView.frame.size.height-24)/2.0, width:24, height:24);
        self.chuangkou_Btn.frame = CGRect(x:self.frame.size.width-64, y:(self.bottomView.frame.size.height-24)/2.0, width:24, height:24);
        self.barrageBtn.frame = CGRect(x:self.chuangkou_Btn.frame.origin.x-28-24, y:(self.bottomView.frame.size.height-24)/2.0, width:24, height:24);
        self.chatBtn.frame = CGRect(x:self.barrageBtn.frame.origin.x-28-24, y:(self.bottomView.frame.size.height-24)/2.0, width:24, height:24);
        if self.isOrientationLandscape { //横屏
            self.multipleBtn.isHidden = false
            self.chatBtn.isHidden = false
            self.barrageBtn.isHidden = false
            self.chuangkou_Btn.isHidden = false
            self.fullScreenBtn.isHidden = true
            self.multipleBtn.frame = CGRect(x:self.chatBtn.frame.origin.x-28-40, y:(self.bottomView.frame.size.height-24)/2.0, width:40, height:24);
            self.chapter.isHidden = false
        } else {
            self.multipleBtn.isHidden = false
            self.chatBtn.isHidden = true
            self.barrageBtn.isHidden = true
            self.chuangkou_Btn.isHidden = true
            self.fullScreenBtn.isHidden = false
            self.multipleBtn.frame = CGRect(x:self.fullScreenBtn.frame.origin.x-24-40, y:(self.bottomView.frame.size.height-24)/2.0, width:40, height:24);
            self.chapter.isHidden = true
        }
        self.chapter.frame = CGRect(x:self.multipleBtn.frame.origin.x-26-35, y:(self.bottomView.frame.size.height-24)/2.0, width:35, height:24);
        if self.isOrientationLandscape {
            self.endLab.frame = CGRect(x:self.chapter.frame.origin.x-24-50, y:(self.bottomView.frame.size.height-24)/2.0, width:50, height:24);
        } else {
            self.endLab.frame = CGRect(x:self.multipleBtn.frame.origin.x-24-50, y:(self.bottomView.frame.size.height-24)/2.0, width:50, height:24);
        }
       
        
        self.slider.frame = CGRect(x:self.startLab.frame.maxX+8, y:(self.bottomView.frame.size.height-8)/2.0, width:self.endLab.frame.origin.x-self.startLab.frame.maxX-16, height:8);

    }
    
    @objc public func ShowmultipleView(show:Bool) {
        UIView.animate(withDuration: 0.25) {[weak self] in
            guard let `self` = self else {
                return
            }
        
         
                if show {
                    if self.isOrientationLandscape {
                        self.multipleView.frame = CGRect(x: self.frame.size.width-280, y: 0, width: 280, height: self.frame.size.height)
                    } else {
                        self.multipleView.frame = CGRect(x: self.frame.size.width-88-30, y: 0, width: 88, height: self.frame.size.height)
                    }
                } else {
                    
                    if self.isOrientationLandscape {
                        self.multipleView.frame = CGRect(x: self.frame.size.width-280, y: self.frame.size.height, width: 280, height: self.frame.size.height)
                    } else {
                        self.multipleView.frame = CGRect(x: self.frame.size.width-88-30, y: self.frame.size.height, width: 88, height: self.frame.size.height)
                    }
                }
        }
    }
    
    @objc public func buttonsAddTarget(target:NSObject, action:Selector) {
        self.backBtn.addTarget(target, action: action, for: .touchUpInside)
        self.downloadBtn.addTarget(target, action: action, for: .touchUpInside)
        self.refreshBtn.addTarget(target, action: action, for: .touchUpInside)
        self.moreBtn.addTarget(target, action: action, for: .touchUpInside)
        self.stopBtn.addTarget(target, action: action, for: .touchUpInside)
        self.chapter.addTarget(target, action: action, for: .touchUpInside)
        self.multipleBtn.addTarget(target, action: action, for: .touchUpInside)
        self.barrageBtn.addTarget(target, action: action, for: .touchUpInside)
        self.chuangkou_Btn.addTarget(target, action: action, for: .touchUpInside)
        self.chatBtn.addTarget(target, action: action, for: .touchUpInside)
        self.fullScreenBtn.addTarget(target, action: action, for: .touchUpInside)
        
      }
    @objc public func playbackMode(Mode:Bool){
        self.isPlaybackMode = Mode;
        if Mode {
    
            self.slider.isHidden = false;

            self.startLab.isHidden = false;
            self.touch = false;
           
            self.multipleBtn .setTitle("1.0X", for: .normal);
            self.multipleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)

            
        }
    }
    
    
   @objc func sliderTap(_ recognizer:UISwipeGestureRecognizer){
       if self.isChange {
           return;
       }
       
       if recognizer.state == UIGestureRecognizer.State.ended {
           
      
           if !self.touch {
              let loc = recognizer.location(in: self.slider)
               let x:Float =  Float(loc.x)
              let r:Float = x / Float (self.slider.frame.size.width);
              let f:Float = self.slider.maximumValue - self.slider.minimumValue
              let value = f * r
             self.slider.setValue(value, animated: true)
            
           }
       }
       
       if self.sliderTapGestureBlock != nil {
           self.sliderTapGestureBlock?(CGFloat(self.slider.value))
       }

       
       self.sliderValueChange(_slider: self.slider)
      
    }
  
    @objc func ControlEventValueChanged(_slider:UISlider) {
      
        self.sliderValueChange(_slider: _slider)
        self.isChange = true;
        self.ShowmultipleView(show: false)  //隐藏倍数图
    }
    //移动过程中
    @objc func sliderValueChange(_slider:UISlider) {
        self.touch = false;
        
        if self.sliderValueChangeBlock != nil {
            self.sliderValueChangeBlock?(CGFloat(slider.value));
        }

    }
    @objc func sliderThumbTouch(_slider:UISlider) {
        self.touch = true;
       
        if self.timer != nil {
            self.timer.invalidate()
            self.timer = nil;
        }
      
    }
    //所有触摸取消事件，即一次触摸因为放上了太多手指而被取消，或者被上锁或者电话呼叫打断。
    @objc func eventTouchChanged(_slider:UISlider) {
        self.sliderThumbUnTouch(_slider: _slider)
    }
    //结束
    @objc func sliderThumbUnTouch(_slider:UISlider) {
        self.touch = false;
        self.isChange = false;
        if self.sliderValueBlock != nil {
            self.sliderValueBlock?(CGFloat(_slider.value))
        }


        
        if self.timer != nil {//如果定时器已经添加,就不能再添加定时器,否则定时速度出错
            return
        }
        

    self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(TF_PPTFunction.timeFireMethod), userInfo: nil, repeats: true)
        RunLoop.main.add(self.timer, forMode: RunLoop.Mode.default)
     
    }
    
    @objc func timeFireMethod() {
        if self.timer != nil{
            self.timer.invalidate()
            self.timer = nil
        }
      
        if self.isPlay {
            self.isHidden = true
        }
       
    }
    
    @objc public func play(isplay:Bool) {
        
        if self.isPlay != isplay || (self.isPlay == false && isplay == false){
            self.isPlay = isplay
//            self.stopBtn.isSelected = true
            if self.timer != nil {
               //如果定时器已经添加,就不能再添加定时器,否则定时速度出错
                return
            }
            
            self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(TF_PPTFunction.timeFireMethod), userInfo: nil, repeats: true)
            RunLoop.main.add(self.timer, forMode: RunLoop.Mode.default)
        }
        
        //在播放
        if self.isPlay == true && self.isHidden == false {
            
            if self.timer != nil {
               //如果定时器已经添加,就不能再添加定时器,否则定时速度出错
                return
            }
            self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(TF_PPTFunction.timeFireMethod), userInfo: nil, repeats: true)
            RunLoop.main.add(self.timer, forMode: RunLoop.Mode.default)
        }
    }
    
    @objc public func setDuration(duration:Float) {
        //不是移动过程中
        if self.isChange == false {
         
            let value :Float = Float(self.slider.value)
            if self.touch == false && fabs(value-duration) > 1.0 {
                self.slider .setValue(Float(duration), animated: true)
            }
        
            self.startLab.text = self.transToHourMinSec(time: duration)
            let totalDuration:Float = self.slider.maximumValue;

            self.endLab.text = self.transToHourMinSec(time: totalDuration)
        }
    }
    
    @objc public func totalTimeLabelShow(isshow:Bool) {
        let duration :Float = self.slider.maximumValue
        self.endLab.text = self.transToHourMinSec(time: duration)
//        self.endLab.isHidden = !isshow
    }
    
  
    @objc public func playbackRate() {
        self.multipleView.subviews.forEach { subviews in
            subviews.removeFromSuperview()
        }
        
        let array = NSMutableArray.init()
        let dict1:Dictionary<String,Any> = ["speed":"0.5","name":"0.5X"]
        let dict2:Dictionary<String,Any> = ["speed":"1.0","name":"1.0X"]
        let dict3:Dictionary<String,Any> = ["speed":"1.25","name":"1.25X"]
        let dict4:Dictionary<String,Any> = ["speed":"1.5","name":"1.5X"]
        let dict5:Dictionary<String,Any> = ["speed":"2.0","name":"2.0X"]
     
        array.add(dict5)
        array.add(dict4)
        array.add(dict3)
        array.add(dict2)
        array.add(dict1)
        let height: CGFloat = (self.multipleView.frame.size.height-20)/CGFloat(array.count)
        for i in 0..<array.count {
          
            let btn111 = TF_VideoDefineBtn.init()
            let dict = array[i] as? Dictionary<String, Any>
            btn111.frame = CGRect(x:0, y:10+CGFloat(i)*height, width:self.multipleView.frame.size.width, height:height);
     
            btn111.titleLabel?.font = UIFont.systemFont(ofSize: 14)
    
            let name = dict?["name"] as? String

            btn111.speed = dict?["speed"] as! String
            btn111.setTitleColor(.white, for: .normal)
            //默认项
            if name == self.multipleBtn.titleLabel?.text {
                btn111.setTitle(name, for: .normal)
                btn111.setTitleColor(UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1), for: .normal)
            } else {
                btn111.setTitle(name, for: .normal)
            }

            btn111.addTarget(self, action: #selector(TF_PPTFunction.playbackRateButtonClick(button:)), for: .touchUpInside)
            self.multipleView.addSubview(btn111)
        }
        
    }
    
    @objc public func initTimer() {
        if self.timer != nil {
           //如果定时器已经添加,就不能再添加定时器,否则定时速度出错
            return
        }
        self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(TF_PPTFunction.timeFireMethod), userInfo: nil, repeats: true)
        RunLoop.main.add(self.timer, forMode: RunLoop.Mode.default)
    }
    
    
    @objc func playbackRateButtonClick(button:TF_VideoDefineBtn) {
        
        //相同的 清晰度不用设置
//        if self.multipleBtn.titleLabel?.text != button.titleLabel?.text {
            
            if self.rateBlock != nil {
                self.ShowmultipleView(show: false)
                let speed = self.StringToFloat(str: button.speed)
                self.rateBlock?(speed)
            }

            
            self.multipleBtn.setTitle(button.titleLabel?.text, for: .normal)
       
            
//        }

        self.multipleView.subviews.forEach { subviews in
            subviews.removeFromSuperview()
        }
      
    }
    
     func StringToFloat(str:String)->(CGFloat){


            let string = str

            var cgFloat:CGFloat = 0

            if let doubleValue = Double(string)

            {

                cgFloat = CGFloat(doubleValue)

            }

            return cgFloat

     }

    func getBottomLayer()  {
        if self.bottom_gl == nil {
            let bgGradient1 = CAGradientLayer()
            bgGradient1.colors = [UIColor(red: 0, green: 0, blue: 0, alpha: 0.4).cgColor, UIColor(red: 0.12, green: 0.12, blue: 0.12, alpha: 0).cgColor, UIColor(red: 0.38, green: 0.38, blue: 0.38, alpha: 0).cgColor]
            bgGradient1.locations = [0, 1, 1]
            bgGradient1.frame = self.bottomView.bounds;
            bgGradient1.startPoint = CGPoint(x: 0.5, y: 1)
            bgGradient1.endPoint = CGPoint(x: 0.5, y: 0)

            self.bottomViewBackgroundColor.layer.addSublayer(bgGradient1)
            self.bottom_gl = bgGradient1;

        }
        self.bottom_gl?.frame = self.bottomViewBackgroundColor.bounds;


     
      
    
    }
    
 
    
    func transToHourMinSec(time: Float) -> String
    {
        let allTime: Int = Int(time)
        var hours = 0
        var minutes = 0
        var seconds = 0
        var hoursText = ""
        var minutesText = ""
        var secondsText = ""
        
        hours = allTime / 3600
        hoursText = hours > 9 ? "\(hours)" : "0\(hours)"
        
        minutes = allTime % 3600 / 60
        minutesText = minutes > 9 ? "\(minutes)" : "0\(minutes)"
        
        seconds = allTime % 3600 % 60
        secondsText = seconds > 9 ? "\(seconds)" : "0\(seconds)"
        
        return "\(hoursText):\(minutesText):\(secondsText)"
    }

    func getTopLayer() -> CAGradientLayer {
        let bgGradient = CAGradientLayer()
        bgGradient.colors = [UIColor(red: 0, green: 0, blue: 0, alpha: 0.6).cgColor, UIColor(red: 0.38, green: 0.38, blue: 0.38, alpha: 0).cgColor]
        bgGradient.locations = [0, 1]
       
        bgGradient.startPoint = CGPoint(x: 0.5, y: 0)
        bgGradient.endPoint = CGPoint(x: 0.5, y: 1)
        self.gl = bgGradient
      
        return gl
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
